<?php
class CV_Unittest_Reporter_Text extends TextReporter{
	private $test_start_times = array();
	function paintPass($message){
		parent::paintPass($message);
		print ".";
		flush();
	}
	
	function paintFail($message) {
		print "\npaintFail\n";
		ob_start();
		parent::paintFail($message);
		echo substr( ob_get_clean(), 0, -1 );
		$test_list = $this->getTestList();
		$test_file = $test_list[1];
		foreach( array_reverse(debug_backtrace()) as $frame ){
			if( $frame['file'] === $test_file ){
				echo ":".$frame['line'];
				break;
			}
		}
		print "\n\n";
		$trace = debug_backtrace();
		foreach( $trace as $key => $frame ){
			if( empty($frame['file']) || substr_count( $frame['file'], CV_SIMPLETEST_DIR ) || substr_count( $frame['file'], CV_DIR ) || substr_count( $frame['file'], 'tests\run.php' ) ){
				continue;
			}
			$format_args = create_function( '$arg', 'return substr(implode(" ",array_map("trim",split("\r\n|\n\r\|\r|\n",print_r($arg,true))), 0, 500));');
			$format_args = create_function( '$arg', 'return substr(print_r($arg,true), 0, 500);');
			echo "#".$key." ".@$frame['class'].@$frame['type'].@$frame['function']."(".implode(",",array_map($format_args,@$frame['args'])).") called at [".@$frame['file'].":".@$frame['line']."]\n";
		}
		print "\n\n";
	}

	function paintError( $message, $trace = array() ) {
		print "\npaintError\n";
		parent::paintError($message,$trace);
		echo substr( ob_get_clean(), 0, -1 );
		$test_list = $this->getTestList();
		$test_file = $test_list[1];
		foreach( array_reverse($trace) as $frame ){
			if( @$frame['file'] === $test_file ){
				echo ":".$frame['line'];
				break;
			}
		}
		print "\n\n";
		foreach( $trace as $key => $frame ){
			if( empty($frame['file']) || substr_count( $frame['file'], CV_SIMPLETEST_DIR ) || substr_count( $frame['file'], CV_DIR ) || substr_count( $frame['file'], 'tests\run.php' ) ){
				continue;
			}
			$format_args = create_function( '$arg', 'return substr(implode(" ",array_map("trim",split("\r\n|\n\r\|\r|\n",print_r($arg,true))), 0, 100));');
			$format_args = create_function( '$arg', 'return substr(print_r($arg,true), 0, 500);');
			echo "#".$key." ".@$frame['class'].@$frame['type'].@$frame['function']."(".implode(",",array_map($format_args,@$frame['args'])).") called at [".@$frame['file'].":".@$frame['line']."]\n";
		}
		print "\n\n";
	}
	
	function paintException(Exception $exception) {
		print "\npaintException\n";
		ob_start();
		parent::paintException($exception);
		echo substr( ob_get_clean(), 0, -1 );
		$test_list = $this->getTestList();
		$test_file = $test_list[1];
		foreach( array_reverse($exception->getTrace()) as $frame ){
			if( $frame['file'] === $test_file ){
				echo ":".$frame['line'];
				break;
			}
		}
		print "\n\n";
		foreach( split("\r\n|\n|\r",$exception->getTraceAsString()) as $line ){
			if( substr_count( $line, CV_SIMPLETEST_DIR ) ){
				break;
			}
			echo $line."\n";
		}
		print "\n\n";
	}
	function paintCaseStart($test_name) {
		$this->test_start_time[$test_name] = microtime( true );
		parent::paintCaseStart($test_name);
		echo $test_name.": ";
	}
	function paintCaseEnd($test_name) {
		parent::paintCaseEnd($test_name);
		$seconds_elaped = microtime( true ) - $this->test_start_time[$test_name];
		$m = floor( $seconds_elaped / 60 );
		$s = floor( $seconds_elaped ) % 60;
		$ms = floor( ($seconds_elaped - floor($seconds_elaped)) * 1000 );
		echo " done in " . ($m?$m.'m ':'') . ($m||$s?$s.'s ':'')  . (!$m&&!$s?$ms.'ms ':'') . "\n";
	}
	function paintSkip($message){
		echo "[skipped]";
	}
}
